import os


def generate_zeros_str(amount: int):
    zero_string = ""
    for _ in range(amount):
        zero_string += "0"
    return zero_string


def get_all_filenames(dirPath):
    """
    获取指定目录下的所有文件名[不含路径]
    :param path: 目录路径
    :return: 文件名列表
    """
    if not os.path.isdir(dirPath):
        raise ValueError(f"路径 {dirPath} 不是一个有效的目录")

    # os.listdir 会返回目录下所有文件和文件夹的名字
    all_items = os.listdir(dirPath)

    # 过滤出文件名[排除文件夹]
    filenames = [
        item for item in all_items if os.path.isfile(os.path.join(dirPath, item))
    ]
    return filenames


def produce_unix_shell(zeroQuantity, folderPath, specificStr):
    filesList = get_all_filenames(folderPath)
    for _, fileName in enumerate(filesList):
        newFileName = str(fileName).replace(specificStr, "")
        newFileNameArr = newFileName.split(".")

        fileNameLen = len(str(newFileNameArr[0]))
        zeroAmount = zeroQuantity - fileNameLen
        zeroPrefixStr = generate_zeros_str(zeroAmount)
        print("# zeroPrefixStr:", zeroPrefixStr)

        previousFilePath = f"/{folderPath}/{fileName}"
        newFileAbsPath = f"/{folderPath}/{zeroPrefixStr}{newFileName}"
        newFileAbsPath = newFileAbsPath.replace("..", ".")

        command = f'mv "{previousFilePath}" "{newFileAbsPath}";'
        command = command.replace("\\", "/")
        command = command.replace(":", "")
        print(command)


folderPathArr = (r"",)
specificStr = "www.internet.com"
zeroQuantity = 4

for _, folderPath in enumerate(folderPathArr):
    produce_unix_shell(
        zeroQuantity=zeroQuantity,
        folderPath=folderPath,
        specificStr=specificStr,
    )
